package manage;

import entity.HotelInfo;
import entity.TravelGuideInfo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class TravelGuideDBManager {

    public static ArrayList<TravelGuideInfo> getAllTravelGuides() {
        ResultSet rs;
        ArrayList<TravelGuideInfo> ret = new ArrayList<TravelGuideInfo>();
        try {
            DBManager dbManager = new DBManager();
            rs = dbManager.queryDB("SELECT * FROM TRAVEL_GUIDE_T");
            while (rs.next()) {
                int id = rs.getInt("ID");
                String title = rs.getString("TITLE");
                String content = rs.getString("CONTENT");
                String imgUrl = "res/TravelGuideImage/" + rs.getString("IMGURL") + ".jpg";
                int likeCount = rs.getInt("LIKECOUNT");
                ret.add(new TravelGuideInfo(id, title, content, imgUrl, likeCount));
            }
            rs.close();
            dbManager.closeConnections();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return ret;
    }

    public static void updateLikeCount(TravelGuideInfo travelGuideInfo, int likeCount) {
        String sql = "UPDATE TRAVEL_GUIDE_T SET LIKECOUNT = " + likeCount + " WHERE ID = " + travelGuideInfo.getId();
        DBManager dbManager = new DBManager();
        dbManager.establishConnection();
        dbManager.updateDB(sql);
        dbManager.closeConnections();
    }
}